package cn.com.controller.wxController;

import cn.com.util.EncryptUtil;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * author: yue
 * date: 2018/01/26
 * description:
 */
@RestController
@RequestMapping("/WX/")
public class JudgeServerController {

    Logger logger= Logger.getLogger(JudgeServerController.class);
    @RequestMapping(value = "judgeServer",method = RequestMethod.GET)
    public String judgeServer(@RequestParam("signature")String signature,
                              @RequestParam("timestamp")String timestamp,
                              @RequestParam("nonce")String nonce,
                              @RequestParam("echostr")String echostr)
    {
        String token="HouseKeeper_Token";
        String[] arr=new String[]{token,timestamp,nonce};
        System.out.println(signature+" "+timestamp+" "+nonce+" "+echostr);
        logger.info(signature+" "+timestamp+" "+nonce+" "+echostr);
        Arrays.sort(arr);
        StringBuilder builder=new StringBuilder();
        for(int i=0;i<arr.length;i++){
            builder.append(arr[i]);
        }
        String encrypt= EncryptUtil.getSha1(new String(builder));
        if (signature.equals(encrypt)){
            return echostr;
        }else {
            return "";
        }
    }
}
